package cn.myschool.service;

import cn.myschool.dao.SubjectDao;
import cn.myschool.entity.Subject;
import cn.myschool.pojo.Page;
import cn.myschool.pojo.Subject2;
import cn.myschool.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class SubjectService {
    public static final Logger Log = LoggerFactory.getLogger(SubjectService.class);
    public static SubjectService ME = new SubjectService();
    public static final int PAGESIZE=10;//默认页大小

    public Page<Subject2> paging(int pageNo, String subjectname){
        List<Subject2> list = this.paging(pageNo, PAGESIZE, subjectname);
        int t = this.searchTotal(subjectname);
        return new Page<>(pageNo,PAGESIZE,t,list);
    }
    public List<Subject2> paging(int pageNo, int pageSize, String subjectname) {
        SqlSession sq = MybatisUtil.get();
        try {
            SubjectDao mapper = sq.getMapper(SubjectDao.class);
            return mapper.paging(pageNo, pageSize,subjectname);
        } finally {
            MybatisUtil.close(sq);
        }
    }
    public int searchTotal(String subjectname) {
        SqlSession sq = MybatisUtil.get();
        try {
            SubjectDao mapper = sq.getMapper(SubjectDao.class);
            return mapper.findTotal(subjectname);
        } finally {
            MybatisUtil.close(sq);
        }
    }
    public  Subject  get(long id) {
        SqlSession sq = MybatisUtil.get();
        try {
            SubjectDao mapper = sq.getMapper(SubjectDao.class);
            return mapper.get(id);
        } finally {
            MybatisUtil.close(sq);
        }
    }
    //
//    public Subject get(long id);
    public int add(Subject s) {
        SqlSession sq = MybatisUtil.get();
        try {
            SubjectDao mapper = sq.getMapper(SubjectDao.class);
            return mapper.add(s);
        } catch (RuntimeException e) {
            //e.printStackTrace();
            Log.error("增加课程失败", e);
        } finally {
            MybatisUtil.close(sq);
        }
        return 0;
    }

    public int del(long id) {
        SqlSession sq = MybatisUtil.get();
        try {
            SubjectDao mapper = sq.getMapper(SubjectDao.class);
            return mapper.del(id);
        } catch (RuntimeException e) {
            //e.printStackTrace();
            Log.error("删除课程失败", e);
        } finally {
            MybatisUtil.close(sq);
        }
        return 0;
    }
    // public int update(Subject s);
    public static void main(String[] args) {
//        List<Subject> paging = SubjectService.ME.paging(1, 20);
//        System.out.println(paging);
    }

    public int update(Subject sub) {
        SqlSession sq = MybatisUtil.get();
        try {
            SubjectDao mapper = sq.getMapper(SubjectDao.class);
            return mapper.update(sub);
        } catch (RuntimeException e) {
            //e.printStackTrace();
            Log.error("修改课程失败", e);
        } finally {
            MybatisUtil.close(sq);
        }
        return 0;
    }
    public boolean checkName(String sub) {
        SqlSession sq = MybatisUtil.get();
        try {
            SubjectDao mapper = sq.getMapper(SubjectDao.class);
            return mapper.checkName(sub)>0;
        } finally {
            MybatisUtil.close(sq);
        }
    }
}
